﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace PeekWebApp.Management.AjaxHandler
{
    /// <summary>
    /// CountriesRegionEditHandler 的摘要说明
    /// </summary>
    public class CountriesRegionEditHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "application/json";
            string action = context.Request["action"];
            if (action.Equals("Edit"))//编辑
            {
                string country = context.Request["country"].Trim();
                string province = context.Request["province"].Trim();
                string city = context.Request["city"].Trim();
                string remark = context.Server.HtmlDecode(context.Request["remark"].Trim());
                string code = context.Request["hidCode"];
                PeekService.CountriesRegion model = new PeekService.CountriesRegionService().GetModel(code);
                if (model == null)
                {
                    model = new PeekService.CountriesRegion();
                    model.Code = Guid.NewGuid().ToString();
                    model.CreateID = "";
                    model.CreateTime = DateTime.Now;
                    model.State = 0;
                    model.Sort = new PeekService.ExtendService().GetSort("CountriesRegion");
                    model.City = city;
                    model.Country = country;
                    model.Province = province;
                    model.Remark = remark;
                    new PeekService.CountriesRegionService().Add(model);
                }
                else
                {
                    model.City = city;
                    model.Country = country;
                    model.Province = province;
                    model.Remark = remark;
                    new PeekService.CountriesRegionService().Update(model);
                }
                context.Response.Redirect("../CountriesRegion/List.aspx");
            }
            else if (action.Equals("Check")) {
                context.Response.ContentType = "text/plain";
                string country = context.Request["country"].Trim();
                string province = context.Request["province"].Trim();
                string city = context.Request["city"].Trim();
                string hidCode = context.Request["hidCode"].Trim();
                PeekService.CountriesRegion m=new PeekService.CountriesRegionService().GetAll().Find(o=>o.Country.Equals(country)&&o.Province.Equals(province)&&o.City.Equals(city));
                if (m != null)
                {
                    if (hidCode.Equals("-1"))
                    {
                        context.Response.Write("false");
                    }
                    else
                    {
                        if (hidCode.Equals(m.Code))
                        {
                            context.Response.Write("true");
                        }
                        else
                        {
                            context.Response.Write("false");
                        }
                    }
                }
                else
                {
                    context.Response.Write("true");
                }
            }
            else //绑定
            {
                string code = context.Request["hidCode"];
                PeekService.CountriesRegion model = new PeekService.CountriesRegionService().GetModel(code);
                context.Response.Write(PeekService.CommonService.ConvertToJSON(model));
            }
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}